#pragma once

#include <vector>

#include "ElectrodeTable.h"
#include "eeg_exceptions.h"
struct Channel{
	int electrodeID;
	std::vector<double> data;
	Electrode electrode;
	Channel(int eid , size_t reserveSize = 0):electrodeID(eid),electrode(eid)
	{
		//data = new std::vector<double>;
		if(reserveSize)
			data.reserve( reserveSize);
	}
	//~Channel(){ delete data;}
};
class DataStore
{
private:
	std::vector<Channel*> data;
	int current; 
	unsigned int samplingRate;
	int refType;//refType=-1 for reset
	Channel* refChannel[3];
	static DataStore *instance;
	DataStore();
public:
	//	DataStore(void);
	~DataStore(void);
	//const Channel& GetChannel(int n) const;
	int AddChannel(int eid, size_t rs);
	int EndChannel(void);//return the size of a ch.
	void CleanAll(void);
	void SetSamplingRate(int s){samplingRate=s;}
	int GetSamplingRate() const{return samplingRate;}
	int GetChannelCount (void) const;
	void SetChannelName(LPCTSTR name);
	void SetElectrodePos(double x, double y, int electrodeID=-1);
	void SetRef(int refType);
	// Get the index of the channel from a given electrodeID. Return -1 if the that electrode is not available.
	int GetElectrodeIndex (int electrodeID) const;
	inline bool GetRefData (int ch, UINT index,double &refData) const//return false if refType==-1. But in that case, refData=data[ch].data[index]
	{
		/*	if(ch>=data.size())
		return false;*/
		if(refType==DATA_REF_TYPE_NONE)
			refData=data[ch]->data[index];
		return false;
		if(refType==DATA_REF_TYPE_A)
		{	
			refData=data[ch]->data[index]-(refChannel[0]->data[index]+refChannel[1]->data[index])/2;
			return true;
		}
		if(refType==DATA_REF_TYPE_C)
		{	
			refData=data[ch]->data[index]-refChannel[2]->data[index];
			return true;
		}
		return false;
	}
	const Channel* GetChannelByEID(unsigned int eid) const;
	LPCTSTR GetRefTypeString() const ;

	inline void AddData2Channel(double d){
		if( current == -1)
			throw EEG_Exceptions(3, _T("DS: Invail Current Channel"));
		data[current]->data.push_back(d);
	}

	static DataStore *GetInstance(){ 
		if(!instance)
			instance = new DataStore();
		return instance;
	}

	static void DestroyInstance(){ 
		if(instance){
			delete instance;
			instance = 0;
		}
	}
};
